package common

import (
	"context"
	commonDto "git.mycaigou.com/gfyx/micro-gfyx-api/infrastructure/dto/common"
	"git.mycaigou.com/gfyx/micro-gfyx-api/infrastructure/repository/common"
	opptyCommonPb "git.mycaigou.com/gfyx/micro-gfyx-api/proto/micro_gfyx_oppty_service_proto/micro-gfyx-oppty-service/common"
	"git.myscrm.cn/golang/stark/v4"
)

type VerifyCodeService struct {
	verifyCodeRepo common.VerifyCodeRepositoryIface
}

// NewVerifyCodeService 构造函数
func NewVerifyCodeService(verifyCodeRepo common.VerifyCodeRepositoryIface) VerifyCodeServiceIface {
	return &VerifyCodeService{
		verifyCodeRepo: verifyCodeRepo,
	}
}

// GetCaptcha 获取验证码
func (v *VerifyCodeService) GetCaptcha(ctx context.Context) (string, string, error) {
	request := &opptyCommonPb.GetCaptchaRequest{}
	response, err := v.verifyCodeRepo.GetCaptcha(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "GetCaptcha service error: %+v", err)
		return "", "", err
	}
	return response.CaptchaId, response.CaptchaImage, nil
}

// SendSms 发送验证码
func (v *VerifyCodeService) SendSms(ctx context.Context, request *commonDto.SendSmsRequest) (*commonDto.SendSmsResponse, error) {
	pbRequest := &opptyCommonPb.SendSmsRequest{
		Scene:         request.Scene,
		Mobile:        request.Mobile,
		CaptchaId:     request.CaptchaID,
		CaptchaAnswer: request.CaptchaAnswer,
	}
	_, err := v.verifyCodeRepo.SendSms(ctx, pbRequest)
	if err != nil {
		stark.Logger.Errorf(ctx, "SendSms service error: %+v", err)
		return nil, err
	}

	return &commonDto.SendSmsResponse{}, nil
}
